package com.ruoyi.web.controller.garde;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.garde.domain.TbGrade;
import com.ruoyi.garde.service.ITbGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 年级Controller
 *
 * @author hongyi
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/garde/grade")
public class TbGradeController extends BaseController
{
    @Autowired
    private ITbGradeService tbGradeService;

    /**
     * 查询年级列表
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbGrade tbGrade)
    {
        startPage();
        List<TbGrade> list = tbGradeService.selectTbGradeList(tbGrade);
        return getDataTable(list);
    }

    /**
     * 导出年级列表
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:export')")
    @Log(title = "年级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbGrade tbGrade)
    {
        List<TbGrade> list = tbGradeService.selectTbGradeList(tbGrade);
        ExcelUtil<TbGrade> util = new ExcelUtil<TbGrade>(TbGrade.class);
        util.exportExcel(response, list, "年级数据");
    }

    /**
     * 获取年级详细信息
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tbGradeService.selectTbGradeById(id));
    }

    /**
     * 新增年级
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:add')")
    @Log(title = "年级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbGrade tbGrade)
    {
        addOEdit(tbGrade,"add");
        return toAjax(tbGradeService.insertTbGrade(tbGrade));
    }

    /**
     * 修改年级
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:edit')")
    @Log(title = "年级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbGrade tbGrade)
    {
        addOEdit(tbGrade,"edit");
        return toAjax(tbGradeService.updateTbGrade(tbGrade));
    }



    public void addOEdit(TbGrade grade, String type) {
        TbGrade code = tbGradeService.getTbGradeByCode(grade.getCode());
        TbGrade name  = tbGradeService.getTbGradeByName(grade.getName());
        if(code!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("学年代码已经存在");
            } else {
                if(!grade.getId().equals(code.getId())) {
                    throw new IllegalArgumentException("学年代码已经存在");
                }
            }
        }
        if(name!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("学年名称已经存在");
            } else {
                if(!grade.getId().equals(name.getId())) {
                    throw new IllegalArgumentException("学年名称已经存在");
                }
            }
        }
    }
    /**
     * 删除年级
     */
    @PreAuthorize("@ss.hasPermi('garde:grade:remove')")
    @Log(title = "年级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbGradeService.deleteTbGradeByIds(ids));
    }
}
